class Solution {
        //定义一个最大的值
        int re = 0;
        int close = 0x7fffffff;
        ArrayList<Integer> list = new ArrayList<>();
    public int threeSumClosest(int[] nums, int target) {
        //动态规划和回溯法
        //定义初始最接近的值
        //分析特殊情况
        if(nums.length <= 3){
            int len = nums.length;
            for(int i = 0;i<len ;i++ ){
                re += nums[i];
            }
            return re;
        }
        //排序
        Arrays.sort(nums);
        clo(3,target,0,0,nums);
        return re;
    }
    //定义目标，最接近的值，数组
    public void clo(int k,int target,int now,int start,int[] nums){
        int len = nums.length;
        if( k == 3) {
            for( int i = start ; i<len-2 ;i++){
                clo(k-1,target,nums[i],i+1,nums);
            }
        }else if( k==2){
            int right = len - 1;
            int left = start;
            while(left < right ){
                int add = nums[left]+nums[right]+now;
                int temp = Math.abs(add - target);
                if(temp < close){
                    re = add;
                    close = temp;
                }
                if(add<target){
                    left++;
                }else{
                    right--;
                }
            }
        }
    }
}

public class MainClass {
    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
          return new int[0];
        }
    
        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for(int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }
    
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            int[] nums = stringToIntegerArray(line);
            line = in.readLine();
            int target = Integer.parseInt(line);
            
            int ret = new Solution().threeSumClosest(nums, target);
            
            String out = String.valueOf(ret);
            
            System.out.print(out);
        }
    }
}